package com.twu.biblioteca;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Anbu
 * Date: 9/23/13
 * Time: 4:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserManager {

    private Map<String, User> userList;
    private int lastCreatedUserId;

    public UserManager() {
        userList = new HashMap<String, User>();
        lastCreatedUserId = 1111110;
    }

    public String createUser(String password, UserDetails userDetails) {
        String userId = parseUserId(++lastCreatedUserId);
        userList.put(userId,new User(userId, password, userDetails));
        return userId;
    }

    private String parseUserId(int userId) {
        String userIdInString = String.valueOf(userId);
        return userIdInString.substring(0,3) + "-" + userIdInString.substring(3,7);
    }

    public User getUserById(String userId) {
        User user = userList.get(userId);
        return (user == null ? User.getNullUser() : user);
    }

    public boolean login(String userId, String password) {
        return getUserById(userId).checkPassword(password);
    }
}
